//
//  RCToplistTypeView.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-27.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCToplistTypeView.h"
#import "RCRecommendFoodTypeView.h"
#import "NSArray+EX.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "NSString+NSString_Ex_.h"

#define kBGimageFile        @"image_searchAllCity_cell_bg.png"
#define kLineImageFile      @"image_line_type3.png"

@interface RCToplistTypeView()

@property(nonatomic,retain)UITableView *typeTableView;
@property(nonatomic,retain)RCTopListTypeDataModel *dataModel;

@end

@implementation RCToplistTypeView

@synthesize typeTableView = _typeTableView;
@synthesize dataModel = _dataModel;

- (id)initWithFrame:(CGRect)frame andDataSource:(RCTopListTypeDataModel *)dataSource
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        if (!_typeTableView) {
            _typeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        }
        _typeTableView.backgroundColor = [UIColor whiteColor];
        _typeTableView.delegate = self;
        _typeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _typeTableView.dataSource = self;
        
        self.dataModel = dataSource;
        
        [self addSubview:_typeTableView];
    }
    return self;
}

-(void)dealloc{
    self.typeTableView.delegate = nil;
    self.typeTableView.dataSource = nil;
    self.typeTableView = nil;
    self.dataModel = nil;
    [super dealloc];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (nil == self.dataModel || nil == self.dataModel.categoryListArray || [self.dataModel.categoryListArray count] == 0) {
        return 0;
    }
    return [self.dataModel.categoryListArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 39;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"RCToplistTypeCell";
    RCPopViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (nil == cell) {
        cell = [[[RCPopViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
    }
    RCTopListTypeCategoryItem *item = [self.dataModel.categoryListArray safeObjectAtIndex:[indexPath row]];
    if (nil == item) {
        return nil;
    }
    [cell setcontentViewWithString:item.categoryName];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    RCTopListTypeCategoryItem *item = [self.dataModel.categoryListArray safeObjectAtIndex:[indexPath row]];
    if (nil == item) {
        return;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(selectToplistTypeItem:)]) {
        [_delegate selectToplistTypeItem:item];
    }
}

@end
